package com.it.controller;


import com.it.service.BlogService;
import com.it.service.BlogTagService;
import com.it.service.BlogTypeService;
import com.it.vo.BlogQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private BlogTypeService typeService;
    @Autowired
    private BlogTagService blogTagService;

    @GetMapping({"/", "index.html"})
    public String helloController1(BlogQueryVo blog, Model model) {
        model.addAttribute("page",blogService.listBlog(new HashMap<>()));
        model.addAttribute("types", typeService.listTypeTop(6));
        model.addAttribute("tags", blogTagService.listTagTop(10));
        model.addAttribute("recommedBlogs", blogService.listRecommedBlogTop(8));
        return "index";
    }

    /**
     * 根据传入的字符串，搜索所有标题或者文章内容包含指定字符串的文章
     *
     * @PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
     */
    @PostMapping("/search")
    public String search(
            String query, Model model) {
        model.addAttribute("page", blogService.listBlogByQuery(new HashMap<>()));
        model.addAttribute("query", query);
        return "search";
    }

    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id, Model model) {
        model.addAttribute("blog", blogService.getBolgAndConvert(id));
        return "blog";
    }

    @GetMapping("/footer/newblog")
    public String newBlogs(Model model) {
        model.addAttribute("newBlogs", blogService.listRecommedBlogTop(3));
        return "_fragments::newBlogList";
    }

}
